package com.example.demo.web;

import com.example.demo.param.dto.news.AddNoticeDto;
import com.example.demo.param.dto.news.InsertNewsDto;
import com.example.demo.param.vo.NewsArrVO;
import com.example.demo.param.vo.NewsVO;
import com.example.demo.service.INewsService;
import com.example.demo.service.ITokenService;
import com.example.demo.util.Json;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "消息")
@RestController
@RequestMapping("news")
public class NewsController extends BaseController{

    @Autowired private INewsService newsService;
    @Autowired private ITokenService tokenService;

    @ApiOperation("新增回复")
    @PostMapping("insert")
    public Json<Boolean> insert(@RequestBody InsertNewsDto dto){
        String uid = this.verification(dto.getToken());
        Boolean exit = this.newsService.insert(dto.getId(),uid,dto.getNewsContent());
        Json<Boolean> json = new Json<>(exit);
        return json;
    }

    @ApiOperation("新增公告")
    @PostMapping("insertNotice")
    public Json<Boolean> insertNotice(@RequestBody AddNoticeDto dto){
        String uid = this.tokenService.isRoleType(dto.getToken(),2);
        Boolean exit = this.newsService.insertNotice(dto.getTitle(), uid, dto.getContent());
        Json<Boolean> json = new Json<>(exit);
        return json;
    }

    @ApiOperation("删除消息")
    @DeleteMapping("delete/message")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "query", dataType = "String")
    })
    public Json<Boolean> delete(@RequestParam String id , @RequestParam String token){
        this.verification(token);
        Boolean exit = this.newsService.delete(id);
        Json<Boolean> json = new Json<>(exit);
        return json;
    }

    @ApiOperation("删除公告")
    @DeleteMapping("delete/notic")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "query", dataType = "String")
    })
    public Json<Boolean> deleteByNotice(@RequestParam String id , @RequestParam String token){
        this.tokenService.isRoleType(token, 2);
        Boolean exit = this.newsService.deleteByNotice(id);
        Json<Boolean> json = new Json<>(exit);
        return json;
    }

    @ApiOperation("获取单条消息详情")
    @GetMapping("getById/{id}")
    public Json<NewsVO> getById(@ApiParam(name = "id", value = "id", required = true)@PathVariable String id){
        NewsVO newsVO = this.newsService.getById(id);
        Json<NewsVO> json = new Json<>(newsVO);
        return json;
    }

    @ApiOperation("获取消息列表")
    @GetMapping("getQueryPage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "newsTitle", value = "消息标题", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "newsContent", value = "消息内容", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "展示的页数", required = true, paramType = "query", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页展示几条", required = true, paramType = "query", dataType = "int", example = "10")
    })
    public Json<NewsArrVO> getQueryPage(
                            @RequestParam(required = false) String userId, @RequestParam(required = false) String newsTitle,
                            @RequestParam(required = false) String newsContent, @RequestParam Integer page,
                            @RequestParam Integer pageSize){
        NewsArrVO newsArrVO = this.newsService.getQueryPage(userId,newsTitle,newsContent,page,pageSize);
        Json<NewsArrVO> json = new Json<>(newsArrVO);
        return json;
    }
}
